********************************************************************************
*                                                                              *
* This do file reproduces all tables related to the IV empirical strategy.     *
*                                                                              *
********************************************************************************

clear matrix
clear mata
clear
set mem 550m
set matsize 11000
set more off

* Change directory (cd) to your working folder path    
* cd "your directory"       

* Data set
use	"IV.dta"

********************************************************************************
*                                    Table 1                                  *
********************************************************************************
* Note:  make sure that you install the following:
* ssc install xtivreg2
* ssc install ranktest

* 1. Regressions with NO covariates
xtset subset

foreach Y of num 75 65 55 45 35 25 {
xtivreg2 autodefensa distance  (violence_est_period2 = emzone) if abs(distance) <= `Y'*1000, first rf saverf saverfprefix(i`Y') savefirst savefprefix(i`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivfinal`Y'
}

* Tables: 
** 1.1 No covariates, reduced form and first stage
**     Columns I through VI; First and Second Panel of Table 1
  #delimit ;
estout i75violence_est_period2 i65violence_est_period2 i55violence_est_period2 i45violence_est_period2 i35violence_est_period2 i25violence_est_period2 i75autodefensa i65autodefensa i55autodefensa i45autodefensa i35autodefensa i25autodefensa using table1a.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N, fmt(%9.3f %9.3f %9.0g) labels(N)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr

** 1.2 No Covariates, second stage:
**     Columns I through VI; Third Panel of Table 1
  #delimit ;
estout ivfinal75 ivfinal65 ivfinal55 ivfinal45 ivfinal35 ivfinal25 using table1b.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


* 2. Regressions with covariates
xtset subset

foreach Y of num 75 65 55 45 35 25 {
xtivreg2 autodefensa autodefensa_1 sendero82 provcap_dist violence_sl_period1 distance  (violence_est_period2 = emzone) if abs(distance)  <= `Y'*1000, first rf saverf saverfprefix(m`Y') savefirst savefprefix(m`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo iv2`Y'
}


** 2.1 First stage and reduced form, with covariates 
**     Columns VII through XII; First and Second Panel of Table 1
  #delimit ;
estout m75violence_est_period2 m65violence_est_period2 m55violence_est_period2 m45violence_est_period2 m35violence_est_period2 m25violence_est_period2 m75autodefensa m65autodefensa m55autodefensa m45autodefensa  m35autodefensa m25autodefensa using table1c.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N, fmt(%9.0f %9.0g %9.0g) labels(N)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


** 2.2 Second stage, with covariates
**     Columns VII through XII; Third Panel of Table 1

  #delimit ;
estout iv275 iv265 iv255 iv245 iv235 iv225 using table1d.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr
*/



********************************************************************************
*                                                                              *
*                          APPENDIX - Tables 2 - 14                            *
*                                                                              *
********************************************************************************


********************************************************************************
*                              Appendix Table 2                                *
********************************************************************************


summarize autodefensa violence_est_period2 sendero82 emzone violence_sl_period1 provcap_dist distance autodefensa_1 mita

label variable sendero82 "Prior Insurgent Presence" 
label variable provcap_dist "Distance to Province Capital"
label variable violence_sl_period1 "Prior Insurgent Violence"
label variable distance "Distance to Em. Zone Border"
label variable emzonedist "Distance x Emergency Zone"
label variable violence_est_period2 "State Violence (Instrumented by Em. Zone)"
label variable autodefensa_1 "Prior Mobilization"
label variable emzone "Emergency Zone"
label variable mita "Mita"

sutex autodefensa violence_est_period2 sendero82 emzone violence_sl_period1 provcap_dist distance autodefensa_1 mita, minmax file(table2app.tex) replace


********************************************************************************
*                              Appendix Table 3                                *
********************************************************************************

tab autodefensa

********************************************************************************
*                              Appendix Table 4                                *
********************************************************************************

**** Manipulation test
**** rddensity implements manipulation testing procedures using the local polynomial density estimators proposed in Cattaneo, Jansson and Ma (2017).  
**** To install rddensity type the following line in the command window 
   * net install rddensity, from(https://sites.google.com/site/rdpackages/rddensity/stata) replace
**** For a review on manipulation testing see McCrary (2008).
   	  
rddensity distance, all


********************************************************************************
*                              Appendix Table 5                               *
********************************************************************************

* Same as main treatment variable but excluding non-lethal violence

* Main results with covariates, second stage
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtivreg2 autodefensa distance autodefensa_1 sendero82 provcap_dist violence_sl_period1 (viol_leth_est_p2 = emzone) if abs(distance)  <= `Y', first rf saverf saverfprefix(a`Y') savefirst savefprefix(a`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo iva`Y'
}

  #delimit ;
estout iva75000 iva65000 iva55000 iva45000 iva35000 iva25000 using table5app.tex, 
cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


********************************************************************************
*                              Appendix Table 6                                *
********************************************************************************

**** Same as main treatment variable (lethal and non-lethal violence), but excluding joint operations

* Main results with covariates, second stage
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtivreg2 autodefensa distance autodefensa_1 sendero82 provcap_dist violence_sl_period1 (violence_est_period2_alt = emzone) if abs(distance) <= `Y', first rf saverfprefix(b`Y') savefirst savefprefix(b`Y')  robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivb`Y'
}

  #delimit ;
estout ivb75000 ivb65000 ivb55000 ivb45000 ivb35000 ivb25000 using table6app.tex, 
cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


********************************************************************************
*                              Appendix Table 7                               *
********************************************************************************

* Main results with covariates, second stage
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtivreg2 autodefensa_noint distance autodefensa_1 sendero82 provcap_dist viol_sl_noint_p1 (violence_est_period2_noint = emzone) if abs(distance) <= `Y', first rf saverf saverfprefix(c`Y') savefirst savefprefix(c`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivc`Y'
}


  #delimit ;
estout ivc75000 ivc65000 ivc55000 ivc45000 ivc35000 ivc25000 using table7app.tex, 
cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


********************************************************************************
*                              Appendix Table 8                                *
********************************************************************************
**** Same as main treatment variable but only lethal insurgent violence

* Main results with covariates, second stage
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtivreg2 autodefensa distance autodefensa_1 sendero82 provcap_dist viol_sl_p1 (violence_est_period2 = emzone) if abs(distance) <= `Y', first rf saverf saverfprefix(d`Y') savefirst savefprefix(d`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivd`Y'
}

  #delimit ;
estout ivd75000 ivd65000 ivd55000 ivd45000 ivd35000 ivd25000 using table8app.tex, 
cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr



********************************************************************************
*                              Appendix Table 9                               *
********************************************************************************

* Main results with Mita, second stage
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtivreg2 autodefensa distance autodefensa_1 sendero82 provcap_dist violence_sl_period1 mita (violence_est_period2 = emzone) if abs(distance) <= `Y', first rf saverf saverfprefix(f`Y') savefirst savefprefix(f`Y') robust fe i(subset) ffirst
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivf`Y'
}

  #delimit ;
estout ivf75000 ivf65000 ivf55000 ivf45000 ivf35000 ivf25000 using table9app.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr
*/


********************************************************************************
*                              Appendix Table 10                                *
********************************************************************************

* Main results with Mita, second stage (LIML)
xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
ivreg2 autodefensa distance autodefensa_1 violence_sl_period1 sendero82 provcap_dist mita subset1 subset2 subset3 (violence_est_period2 = emzone) if abs(distance) <= `Y', first robust liml
mat first=e(first)
estadd scalar fs = first[4,1] 
eststo ivg`Y'
}

  #delimit ;
estout ivg75000 ivg65000 ivg55000 ivg45000 ivg35000 ivg25000 using
table10app.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr

********************************************************************************
*                              Tables 11 and 12 see R code                       
********************************************************************************


********************************************************************************
*                              Appendix Table 13                             *
********************************************************************************

*** OLS regression 
sort id
preserve 

xtset subset

foreach Y of num 75000 65000 55000 45000 35000 25000 {
xtreg autodefensa violence_est_period2 distance autodefensa_1 sendero82 provcap_dist violence_sl_period1 if abs(distance) <= `Y', robust fe 
mat first=e(first)
*estadd scalar fs = first[4,1] 
eststo ivh`Y'
}


  #delimit ;
estout ivh75000 ivh65000 ivh55000 ivh45000 ivh35000 ivh25000 using table13app.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N r2, fmt(%9.0f %9.0g %9.0g) labels(N)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(OLS Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr
restore



********************************************************************************
*                              Appendix Table 15                               *
********************************************************************************

*** Cluster Errors at district level,  no mita
* Finlay, K., Magnusson, L.M., Schaffer, M.E. 2013.  weakiv:  Weak-instrument-robust tests and confidence intervals for instrumental-variable (IV) estimation of linear, probit and tobit models.  http://ideas.repec.org/c/boc/bocode/s457684.html
* To replicate this table you will need to install:
*ssc install weakiv 
*ssc install weakivtest
* ssc install avar 


* Cluster Errors at district level,  no mita
foreach Y of num 75 65 55 45 35 25 {
ivreg2 autodefensa autodefensa_1 sendero82 provcap_dist violence_sl_period1 distance subset1 subset2 subset3 (violence_est_period2 = emzone) if abs(distance) <= `Y'*1000, first rf saverf saverfprefix(z`Y') savefirst savefprefix(z`Y') robust cluster(ccdi) ffirst
*mat first=e(first)
weakiv 
quietly ivreg2 autodefensa autodefensa_1 sendero82 provcap_dist violence_sl_period1 distance subset1 subset2 subset3 (violence_est_period2 = emzone) if abs(distance) <= `Y'*1000, first rf saverf saverfprefix(z`Y') savefirst savefprefix(z`Y') robust cluster(ccdi) ffirst
mat first=e(first)
* Montiel-Pflueger F statistics:
estadd scalar fs = first[8,1] 
weakivtest
eststo t14z`Y'
}

* Cluster Errors at district level,  with mita
foreach Y of num 75 65 55 45 35 25 {
ivreg2 autodefensa autodefensa_1 sendero82 provcap_dist violence_sl_period1 distance  mita subset1 subset2 subset3 (violence_est_period2 = emzone) if abs(distance) <= `Y'*1000, first rf saverf saverfprefix(w`Y') savefirst savefprefix(w`Y') robust cluster(ccdi) ffirst
*mat first=e(first)
weakiv 
quietly ivreg2 autodefensa autodefensa_1 sendero82 provcap_dist violence_sl_period1 distance  mita subset1 subset2 subset3 (violence_est_period2 = emzone) if abs(distance) <= `Y'*1000, first rf saverf saverfprefix(w`Y') savefirst savefprefix(w`Y') robust cluster(ccdi) ffirst
mat first=e(first)
* Montiel-Pflueger F statistics:
estadd scalar fs = first[8,1] 
weakivtest
eststo t14w`Y'
}

**** Panel 1 of Table 15
  #delimit ;
estout z75violence_est_period2 z65violence_est_period2 z55violence_est_period2 z45violence_est_period2 z35violence_est_period2 z25violence_est_period2 w75violence_est_period2 w65violence_est_period2 w55violence_est_period2 w45violence_est_period2 w35violence_est_period2 w25violence_est_period2 using table15firstapp.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N, fmt(%9.0f %9.0g %9.0g) labels(N)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(First Stage)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr

**** Panel 2 of Table 15
  #delimit ;
estout z75autodefensa z65autodefensa z55autodefensa z45autodefensa z35autodefensa z25autodefensa w75autodefensa w65autodefensa w55autodefensa w45autodefensa w35autodefensa w25autodefensa using table15reducedapp.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N, fmt(%9.0f %9.0g %9.0g) labels(N)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(Reduced Form)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{lllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr

**** Panel 3 of Table 15
  #delimit ;
estout t14z75 t14z65 t14z55 t14z45 t14z35 t14z25 t14w75 t14w65 t14w55 t14w45 t14w35 t14w25 using table15secondapp.tex, cells(b(star fmt(%9.3f)) 
se(par)) stats(N fs, fmt(%9.0f %9.0g %9.0g) labels(N F)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)
label title(IV Estimates)  collabels(, none) varlabels(_cons Constant) style(tex)prehead("\begin{tabular}{llllllllllllll}" \hline\hline) 
posthead(\hline) prefoot(\hline) postfoot(\hline\hline "\end{tabular}")
replace;
#delimit cr


********************************************************************************
*                              Appendix Table 14                            *
********************************************************************************

preserve

sort ccdi 

by ccdi: egen ave_autodefensadi=mean(autodefensa)

by ccdi: generate obs_num=_n 

drop if obs_num~=1 

ttest ave_autodefensadi, by(emzone) unequal

restore
